# -*- coding:utf-8 -*- ＃
'''
Created on 2012-8-2

'''
from data_access.base_data_access import BaseDataAccess
from flask import json

class UserDataAccess(BaseDataAccess):
    def __init__(self):
        pass;
    
    def get_user_by_id(self, user_id):
        """Gets the specified user info by user id
        """
        result = None;
        connection = self.new_db_connection();
        with connection:
            cursor = connection.cursor();
            try:
                count = cursor.execute("select * from User where Id = %s limit 1", user_id);
                row = cursor.fetchone();
                
                if row != None and count > 0:
                    result = {"Id": str(row[0]), "DisplayName": str(row[1]), "PhoneNumber": str(row[2])};
            finally: 
                cursor.close();
                
        return json.dumps(result, indent=4);
    
    def add_user(self, user):
        """Create a new user
        """
        connection = self.new_db_connection();
        with connection:
            cursor = connection.cursor();
            
            try:
                count = cursor.execute("insert into User(DisplayName, PhoneNumber, CreatedTime) values(%s, %s, NOW())",
                                       (str(user["DisplayName"]), str(user["PhoneNumber"])));
            finally:
                cursor.close();
                
            return count;
